package edu.drexel.cs575.phw1.scale;

import java.util.ArrayList;
import java.util.List;

import listeners.KeypadListener;

/**
 * Accepts user input via the use of buttons. For the purpose of this exercise,
 * it is assumed that the Keypad is only to be used with scales (so that I don't
 * have to set up a Keypad hierarchy).
 * 
 * @author anthony
 *
 */
public class Keypad {
	private List<KeypadListener> listeners = new ArrayList<KeypadListener>();
	
	public void pushButton(Button button) {
		if (button == null) {
			throw new IllegalArgumentException("Button cannot be null");
		}
		
		System.out.println(String.format("KEYPAD: %s", button.toString()));
		
		switch (button) {
		case TOGGLE_UNITS:
			notifyUnitsButtonPushedListeners();
			
			break;
			
		case TARE:
			notifyTareButtonPushedListeners();
			
			break;
			
		case BEEP:
			notifyBeepButtonPushedListeners();
		}
	}
	
	public void addListener(KeypadListener listener) {
		if (listener == null) {
			throw new IllegalArgumentException("Listener cannot be null");
		}
		
		this.listeners.add(listener);
	}
	
	private void notifyUnitsButtonPushedListeners() {
		for(KeypadListener listener : listeners) {
			listener.unitsButtonPushed();
		}
	}
	
	private void notifyTareButtonPushedListeners() {
		for(KeypadListener listener : listeners) {
			listener.tareButtonPushed();
		}
	}
	
	private void notifyBeepButtonPushedListeners() {
		for(KeypadListener listener : listeners) {
			listener.beepButtonPushed();
		}
	}
}
